/*******************************************************************************************************************
 * file : input.js
 *
 * author : Nicolas DAURES
 * date : 17/02/2014
 *
 * brief : input
 *******************************************************************************************************************/


/****************************************************************
 * Keyboard
 ****************************************************************/

// Input state
var InputState = {NONE:0, GOTO_PRESSED:1, PRESSED:2, HOLD:3, GOTO_RELEASED:4, RELEASED:5}

// Keyboard state
function Keyboard()
{
	//=======================
	// Fields
	//=======================

	this.m_aKeys =	new Array(222);
}


//=======================
// Accessors
//=======================

Keyboard.prototype.getKeyState = function(a_KeyIndex)
{
    return this.m_aKeys[a_KeyIndex];
}


//=======================
// Operations
//=======================

Keyboard.prototype.update = function()
{
    for (var i = 0; i < this.m_aKeys.length; i++)
    {
        if (this.m_aKeys[i] == InputState.GOTO_PRESSED)
        {
            this.m_aKeys[i] = InputState.PRESSED;
        }
        else if (this.m_aKeys[i] == InputState.PRESSED)
        {
            this.m_aKeys[i] = InputState.HOLD;
        }
        else if (this.m_aKeys[i] == InputState.GOTO_RELEASED)
        {
            this.m_aKeys[i] = InputState.RELEASED;
        }
        else if (this.m_aKeys[i] == InputState.RELEASED)
        {
            this.m_aKeys[i] = InputState.NONE;
        }
    }
}

Keyboard.m_Instance = null;
Keyboard.getInstance = function()
{
    if (this.m_Instance == null)
    {
		this.m_Instance = new Keyboard();
    }

    return this.m_Instance;
}


/****************************************************************
 * Mouse
 ****************************************************************/

// Mouse state
function Mouse()
{
	//=======================
	// Fields
	//=======================

	this.m_aButtons =	new Array(3);
	this.m_v2Pos =	    new b2Vec2(0, 0);
}


//=======================
// Accessors
//=======================

Mouse.prototype.getMouseButtonState = function(a_MouseButtonIndex)
{
    return this.m_aButtons[a_MouseButtonIndex];
}


//=======================
// Operations
//=======================

Mouse.prototype.update = function()
{
    for (var i = 0; i < this.m_aButtons.length; i++)
    {
        if (this.m_aButtons[i] == InputState.GOTO_PRESSED)
        {
            this.m_aButtons[i] = InputState.PRESSED;
        }
        else if (this.m_aButtons[i] == InputState.PRESSED)
        {
            this.m_aButtons[i] = InputState.HOLD;
        }
        else if (this.m_aButtons[i] == InputState.GOTO_RELEASED)
        {
            this.m_aButtons[i] = InputState.RELEASED;
        }
        else if (this.m_aButtons[i] == InputState.RELEASED)
        {
            this.m_aButtons[i] = InputState.NONE;
        }
    }
}

Mouse.m_Instance = null;
Mouse.getInstance = function()
{
    if (this.m_Instance == null)
    {
		this.m_Instance = new Mouse();
    }

    return this.m_Instance;
}


/****************************************************************
 * Input Engine
 ****************************************************************/

// Input manager
function InputEngine()
{
	//=======================
	// Fields
	//=======================

	this.m_Keyboard =	Keyboard.getInstance();
	this.m_Mouse =		Mouse.getInstance();
}


//=======================
// Operations
//=======================

InputEngine.prototype.update = function()
{
    this.m_Keyboard.update();
    this.m_Mouse.update();
}

InputEngine.m_Instance = null;
InputEngine.getInstance = function()
{
    if (this.m_Instance == null)
    {
		this.m_Instance = new InputEngine();

        for (var i = 0; i < this.m_Instance.m_Keyboard.m_aKeys.length; i++)
        {
            this.m_Instance.m_Keyboard.m_aKeys[i] = InputState.NONE;
        }
        for (var i = 0; i < this.m_Instance.m_Mouse.m_aButtons.length; i++)
        {
            this.m_Instance.m_Mouse.m_aButtons[i] = InputState.NONE;
        }
    }

    return this.m_Instance;
}


/****************************************************************
 * Input functions
 ****************************************************************/

// Keyboard event listener
function onKeyDown(a_Event)
{
    Keyboard.getInstance().m_aKeys[a_Event.keyCode] = InputState.GOTO_PRESSED;
}

// Keyboard event listener
function onKeyUp(a_Event)
{
    Keyboard.getInstance().m_aKeys[a_Event.keyCode] = InputState.GOTO_RELEASED;
}

// Mouse event listener
function onMouseDown(a_Event)
{
    Mouse.getInstance().m_aButtons[a_Event.button] = InputState.GOTO_PRESSED;
}

// Mouse event listener
function onMouseUp(a_Event)
{
    Mouse.getInstance().m_aButtons[a_Event.button] = InputState.GOTO_RELEASED;
}

// Mouse event listener
function onMouseMove(a_Event)
{
    var v2MousePos = Mouse.getInstance().m_v2Pos;
    
    if (a_Event.pageX || a_Event.pageY)
    { 
		v2MousePos.x = a_Event.pageX;
		v2MousePos.y = a_Event.pageY;
    }
    else
    { 
		v2MousePos.x = a_Event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; 
		v2MousePos.y = a_Event.clientY + document.body.scrollTop + document.documentElement.scrollTop; 
    }
    
    var v2Offset = new b2Vec2();
    v2Offset.x = 0;
    v2Offset.y = 0;
    var canvas = document.getElementById('canvas');    
    var currentElement = canvas;
    do
    {
        v2Offset.x += currentElement.offsetLeft;
        v2Offset.y += currentElement.offsetTop;
    }
    while(currentElement = currentElement.offsetParent)

    v2MousePos.x -= v2Offset.x;
    v2MousePos.y -= v2Offset.y;
}
